//
//  VersionViewController.m
//  Leo01
//
//  Created by 张旭 on 15/9/4.
//  Copyright (c) 2015年 张旭. All rights reserved.
//

#import "VersionViewController.h"
#import "SyncManager.h"
#import "LeoMsg.h"
@interface VersionViewController ()
@property(nonatomic,strong) NSString *updateUrl;
@end

@implementation VersionViewController
@synthesize navLeftButton;
@synthesize updateUrl;
-(void)dealloc
{
    updateUrl=NULL;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.extendedLayoutIncludesOpaqueBars = YES;
    self.title=@"关于";
    
    [self initNavButton];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if(self.view.window==nil)
    {
        self.view=nil;
    }
}

- (void)initNavButton {
    [navLeftButton setTarget:self];
    [navLeftButton setAction:@selector(back)];
}

- (void)back{
    //[self.navigationController popViewControllerAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)checkVersion:(id)sender {
    SyncManager *syncManager=[[SyncManager alloc] init];
    if(syncManager==NULL)
        return;
    [syncManager checkAppVersion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        if(result==SUCESS)
        {
            if(retValue!=NULL&&[retValue valueForKey:@"isLatest"]!=NULL&&[[retValue valueForKey:@"isLatest"] boolValue]==NO)
            {
                Version *version=[retValue valueForKey:@"version"];
                if(version==NULL)
                    return ;
                //询问用户是否升级
                NSString *title=[NSString stringWithFormat:@"检测到app有新版本%@,版本大小为%.2f，是否现在前往升级?",version.fwVer,version.fileSize/(1024*1024)];
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                                message:title
                                                               delegate:self
                                                      cancelButtonTitle:@"取消"
                                                      otherButtonTitles:@"确定",nil];
                [alert show];
                alert=nil;
                self.updateUrl=version.url;
            }
            else
            {
                [LeoMsg showMsg:@"您的app已是最新版本"];
                return;
            }
        }
        else
        {
            if(description!=NULL)
                [LeoMsg showMsg:description];
            else
                [LeoMsg showMsg:@"您的app已是最新版本"];
        }

    }];
   

}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex==1)
    {
        
            NSURL *url=[NSURL URLWithString:self.updateUrl];
            [[UIApplication sharedApplication] openURL:url];
    }
}

@end
